#include "loginwin.h"
#include "ui_loginwin.h"
#include "mainwindow.h"
#include <QString>
#include <QMessageBox>
loginWin::loginWin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::loginWin)
{
    ui->setupUi(this);
    ui->serverAddress->setText("127.0.0.1:8080");
    ui->userName->setText("square");
    ui->password->setText("666666");
}

loginWin::~loginWin()
{
    delete ui;
}
void loginWin::replyFinish(QNetworkReply* reply)
{

    QString rp = reply->readAll();
    if(rp == "success"){
        MainWindow *m = new MainWindow();
        m->show();
        this->close();
    }
}

//login
void loginWin::on_pushButton_clicked()
{
    QString serverAdd = ui->serverAddress->text();
    QString username = ui->userName->text();
    QString passwd = ui->password->text();

    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    QNetworkRequest request;
    request.setUrl(QUrl("http://"+serverAdd+"/auth?name=" + username + "&passwd=" + passwd));
    QNetworkReply* reply = manager->get(request);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinish(QNetworkReply*)));

}



//quit program
void loginWin::on_pushButton_2_clicked()
{
    QMessageBox message(QMessageBox::Warning,"Information","Sure to quit?",QMessageBox::Yes|QMessageBox::No,NULL);
    if (message.exec()==QMessageBox::Yes){
        qDebug()<<"clicked yes\n";

        this->close();
    }
    else {
        qDebug()<<"clicked no\n";
    }

}
